{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 3, 6]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table><tbody><tr><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td><td style='text-align:center;vertical-align:middle; margin: 0.5em;border:1px #90999f solid;border-collapse:collapse'><img style='max-width: 100px; max-height:100px;display:inline' src=\"\"></td></tr></tbody></table><div><small>(a vector displayed as a row to save space)</small></div>"
      ],
      "text/plain": [
       "3-element Array{Base.ReinterpretArray{Gray{Float32},2,Float32,Array{Float32,2}},1}:\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]\n",
       " [Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); … ; Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0); Gray{Float32}(0.0f0) Gray{Float32}(0.0f0) … Gray{Float32}(0.0f0) Gray{Float32}(0.0f0)]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Take a look at some random images and labels in data\n",
    "using Knet,Images,Random,MLDatasets\n",
    "xtrn,ytrn = MNIST.traindata(Float32); ytrn[ytrn .== 0] .= 10\n",
    "xtst,ytst = MNIST.testdata(Float32);  ytst[ytst .== 0] .= 10\n",
    "rp = randperm(10000)\n",
    "println(ytst[rp[1:3]])\n",
    "[ MNIST.convert2image(xtst[:,:,rp[i]]) for i in 1:3 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Main.MLP"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "include(\"mlp.jl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "This example learns to classify hand-written digits from the \\href{http://yann.lecun.com/exdb/mnist}{MNIST} dataset.  There are 60000 training and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image.  The pixel values are normalized to [0,1]. Each output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class (0-9) for a given image.  10 is used to represent 0.\n",
       "\n",
       "You can run the demo using \\texttt{julia mlp.jl} on the command line or \\texttt{julia> MLP.main()} at the Julia prompt.  Options can be used like \\texttt{julia mlp.jl --epochs 3} or \\texttt{julia> MLP.main(\"--epochs 3\")}.  Use \\texttt{julia mlp.jl --help} for a list of options.  The dataset will be automatically downloaded.  By default a softmax model will be trained for 10 epochs.  You can also train a multi-layer perceptron by specifying one or more –hidden sizes.  The accuracy for the training and test sets will be printed at every epoch and optimized parameters will be returned.\n",
       "\n"
      ],
      "text/markdown": [
       "This example learns to classify hand-written digits from the [MNIST](http://yann.lecun.com/exdb/mnist) dataset.  There are 60000 training and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image.  The pixel values are normalized to [0,1]. Each output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class (0-9) for a given image.  10 is used to represent 0.\n",
       "\n",
       "You can run the demo using `julia mlp.jl` on the command line or `julia> MLP.main()` at the Julia prompt.  Options can be used like `julia mlp.jl --epochs 3` or `julia> MLP.main(\"--epochs 3\")`.  Use `julia mlp.jl --help` for a list of options.  The dataset will be automatically downloaded.  By default a softmax model will be trained for 10 epochs.  You can also train a multi-layer perceptron by specifying one or more –hidden sizes.  The accuracy for the training and test sets will be printed at every epoch and optimized parameters will be returned.\n"
      ],
      "text/plain": [
       "  This example learns to classify hand-written digits from the MNIST (http://yann.lecun.com/exdb/mnist) dataset. There are 60000 training\n",
       "  and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image. The pixel values are normalized to [0,1]. Each\n",
       "  output y is converted to a ten-dimensional one-hot vector (a vector that has a single non-zero component) indicating the correct class\n",
       "  (0-9) for a given image. 10 is used to represent 0.\n",
       "\n",
       "  You can run the demo using \u001b[36mjulia mlp.jl\u001b[39m on the command line or \u001b[36mjulia> MLP.main()\u001b[39m at the Julia prompt. Options can be used like \u001b[36mjulia\n",
       "  mlp.jl --epochs 3\u001b[39m or \u001b[36mjulia> MLP.main(\"--epochs 3\")\u001b[39m. Use \u001b[36mjulia mlp.jl --help\u001b[39m for a list of options. The dataset will be automatically\n",
       "  downloaded. By default a softmax model will be trained for 10 epochs. You can also train a multi-layer perceptron by specifying one or\n",
       "  more –hidden sizes. The accuracy for the training and test sets will be printed at every epoch and optimized parameters will be\n",
       "  returned."
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@doc MLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: <PROGRAM> [--seed SEED] [--batchsize BATCHSIZE]\n",
      "                 [--epochs EPOCHS] [--hidden [HIDDEN...]] [--lr LR]\n",
      "                 [--winit WINIT] [--fast] [--atype ATYPE]\n",
      "                 [--gcheck GCHECK]\n",
      "\n",
      "mlp.jl (c) Deniz Yuret, 2016. Multi-layer perceptron model on the\n",
      "MNIST handwritten digit recognition problem from\n",
      "http://yann.lecun.com/exdb/mnist.\n",
      "\n",
      "optional arguments:\n",
      "  --seed SEED           random number seed: use a nonnegative int for\n",
      "                        repeatable results (type: Int64, default: -1)\n",
      "  --batchsize BATCHSIZE\n",
      "                        minibatch size (type: Int64, default: 100)\n",
      "  --epochs EPOCHS       number of epochs for training (type: Int64,\n",
      "                        default: 10)\n",
      "  --hidden [HIDDEN...]  sizes of hidden layers, e.g. --hidden 128 64\n",
      "                        for a net with two hidden layers (type: Int64)\n",
      "  --lr LR               learning rate (type: Float64, default: 0.5)\n",
      "  --winit WINIT         w initialized with winit*randn() (type:\n",
      "                        Float64, default: 0.1)\n",
      "  --fast                skip loss printing for faster run\n",
      "  --atype ATYPE         array type: Array for cpu, KnetArray for gpu\n",
      "                        (default: \"KnetArray{Float32,N} where N\")\n",
      "  --gcheck GCHECK       check N random gradients per parameter (type:\n",
      "                        Int64, default: 0)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "MLP.main(\"--help\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mlp.jl (c) Deniz Yuret, 2016. Multi-layer perceptron model on the MNIST handwritten digit recognition problem from http://yann.lecun.com/exdb/mnist.\n",
      "opts=(:batchsize, 100)(:fast, false)(:atype, \"KnetArray{Float32,N} where N\")(:epochs, 10)(:gcheck, 0)(:winit, 0.1)(:lr, 0.5)(:hidden, Int64[])(:seed, -1)\n",
      "(:epoch, 0, :trn, 0.106, :tst, 0.0991)\n",
      "(:epoch, 1, :trn, 0.9006333333333333, :tst, 0.9038)\n",
      "(:epoch, 2, :trn, 0.9082333333333333, :tst, 0.9083)\n",
      "(:epoch, 3, :trn, 0.9111, :tst, 0.9099)\n",
      "(:epoch, 4, :trn, 0.9131666666666667, :tst, 0.9109)\n",
      "(:epoch, 5, :trn, 0.9148166666666666, :tst, 0.913)\n",
      "(:epoch, 6, :trn, 0.9162, :tst, 0.9143)\n",
      "(:epoch, 7, :trn, 0.9173666666666667, :tst, 0.9145)\n",
      "(:epoch, 8, :trn, 0.9181166666666667, :tst, 0.9151)\n",
      "(:epoch, 9, :trn, 0.9186166666666666, :tst, 0.9149)\n",
      "(:epoch, 10, :trn, 0.9192833333333333, :tst, 0.9153)\n",
      " 17.133534 seconds (37.27 M allocations: 5.757 GiB, 3.03% gc time)\n"
     ]
    }
   ],
   "source": [
    "model = MLP.main(\"\");"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.5.3",
   "language": "julia",
   "name": "julia-1.5"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
